package com.kyzh.core.pager.weal.moneycard

import android.graphics.Color
import android.os.Bundle
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gushenge.core.beans.CouponXX
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.requests.WealRequest
import com.kyzh.core.R
import com.kyzh.core.activities.BrowserActivity
import com.kyzh.core.databinding.BottomdialogMoneycardBinding
import com.kyzh.core.databinding.FragMoneycardBinding
import com.kyzh.core.databinding.ItemQuanBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.toast
import com.kyzh.core.utils.startActivity

//省钱卡
class FragmentMoneyCard : BaseFragment() {
    private var adapter :Adapter?=null
    private var daySelect = 0
    private lateinit var db : FragMoneycardBinding
    private var mZhouData  = arrayListOf<CouponXX>()
    private var mYueData  = arrayListOf<CouponXX>()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        db = FragMoneycardBinding.inflate(inflater)
        return db.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.initClick()
        db.rev .layoutManager = LinearLayoutManager(requireContext(),RecyclerView.HORIZONTAL,false)

    }
    fun getData(){
        WealRequest.moneyCard {
            db.data = this
            this?.apply {
                mZhouData= this.zhouData.coupon
                mYueData= this.yueData.coupon
                adapter = Adapter(this.isBuy,daySelect)
                db.rev .adapter = adapter

                adapter?.setNewInstance(mZhouData)

                daySelect= when (db.data?.isBuy) {
                    1 -> {
                        0
                    }
                    2 -> {
                        1
                    }
                    else -> {
                        0
                    }
                }
                db.select = daySelect
                zhuangtai()
                // 剩余天数
                if(isBuy!=0){
                    db.tvShengYuTime.text = "剩余 $shengYuDay 天到期"
                }
                db. content.text = Html.fromHtml( content)
            }
        }
    }
    override fun onResume() {
        super.onResume()
        getData()
    }
    private fun FragMoneycardBinding.initClick() {
        view3.setOnClickListener {
            daySelect = 0
            db.select = daySelect
            zhuangtai()
        }
        view4.setOnClickListener {
            daySelect = 1
            db.select = daySelect
            zhuangtai()
        }
        close.setOnClickListener {
            requireActivity().finish()
        }
        tvSubmit.setOnClickListener {
            val inflate = BottomdialogMoneycardBinding.inflate(layoutInflater)
            val bottomSheetDialog = BottomSheetDialog(requireContext(),R.style.BottomSheetDialogStyle)

            inflate.tvAli.setOnClickListener {
                var katype = if(daySelect ==1){
                    "4"
                } else   {
                    "3"
                }
                WealRequest.moneyCardPay(katype,"3"){
                    this?.apply {
                        startActivity<BrowserActivity>(GlobalKeys.TITLE to "支付", GlobalKeys.LINK to this.url)
                    }
                }
            }
            inflate.tvWX.setOnClickListener {
                //2支付类型（2微信支付3支付宝支付）
                //1 省钱卡类型1省钱卡周卡2省钱卡月卡3超级省钱卡周卡4超级省钱卡月卡
                var katype = if(daySelect ==1){
                    "4"
                } else   {
                    "3"
                }
                WealRequest.moneyCardPay(katype,"2"){
                    this?.apply {
                        startActivity<BrowserActivity>(GlobalKeys.TITLE to "支付", GlobalKeys.LINK to this.url)
                    }
                }
            }
            inflate.cancel.setOnClickListener {
                bottomSheetDialog.dismiss()
            }
            bottomSheetDialog.setContentView(inflate.root)
            bottomSheetDialog.show()
        }
    }

    private fun zhuangtai() {



        adapter = Adapter(db.data?.isBuy,daySelect)
        db.rev .adapter = adapter
        adapter?.setOnItemChildClickListener { adapter, view, position ->
            val coupon = adapter.data[position] as CouponXX
            if(view.id == R.id.tvStatus){
                WealRequest.getCardCoupon(coupon.id,coupon.day){
                    coupon.isLq = 1
                    adapter.notifyItemChanged(position)
                }
            }
        }
        when(daySelect){
            1 ->{
                adapter?.setNewInstance(mYueData)
                db. tv3.text = String.format(resources.getString(R.string.moneycard4),db.data?.yueData?.zongji)
                db. tv2.text= "开通超级省钱卡，${db.data?.yueData?.useNumDay} 天内均可使用"
                db.tvSubmit.text = when(db.data?.isBuy){
                    1->{
                        db.tvSubmit.isEnabled = false
                        db.tvSubmit.setBackgroundResource(R.drawable.gradient_ligray)
                        "已开通7天周卡"
                    }
                    2->{
                        db.tvSubmit.isEnabled = false
                        db.tvSubmit.setBackgroundResource(R.drawable.gradient_ligray)
                        "已开通30天月卡"
                    }
                    else ->{
                        db.tvSubmit.isEnabled = true
                        " ${db.data?.yueData?.jiage}开通省钱卡"
                    }
                }
            }
            else ->{
                adapter?.setNewInstance(mZhouData)
                db. tv3.text = String.format(resources.getString(R.string.moneycard4),db.data?.zhouData?.zongji)
                db. tv2.text= "开通超级省钱卡，${db.data?.zhouData?.useNumDay} 天内均可使用"
                db.tvSubmit.text = when(db.data?.isBuy){
                    1->{
                        db.tvSubmit.isEnabled = false
                        db.tvSubmit.setBackgroundResource(R.drawable.gradient_ligray)
                        "已开通7天周卡"
                    }
                    2->{
                        db.tvSubmit.isEnabled = false
                        db.tvSubmit.setBackgroundResource(R.drawable.gradient_ligray)
                        "已开通30天月卡"
                    }
                    else ->{
                        db.tvSubmit.isEnabled = true

                        " ${db.data?.zhouData?.jiage}开通省钱卡"
                    }
                }
            }
        }
    }


    private  class Adapter(var vip: Int?,var daySelect:Int) :BaseQuickAdapter<CouponXX, BaseDataBindingHolder<ItemQuanBinding>>(R.layout.item_quan){
        init {
            addChildClickViewIds(R.id.tvStatus)
        }
        override fun convert(holder: BaseDataBindingHolder<ItemQuanBinding>, item: CouponXX) {
            holder.dataBinding?.apply {
                data = item
                if(vip == 0 ){
                    if(holder.position==0){
                        tvStatus.text = item.ktjsName
                    }else{
                        tvStatus.text = item.mrzlName
                    }
                    tvStatus.isEnabled = false
                    tvStatus.background = null
                    tvStatus.setTextColor(Color.parseColor("#EEEEEE"))
                }else{
                    if((vip ==1&&daySelect ==0  )||(vip ==2&&daySelect ==1  )){
                        if(item.isLq == 0){
                            tvStatus.text = "领取"
                            tvStatus.setBackgroundResource(R.drawable.gradient_fa8c08)
                            tvStatus.setTextColor(Color.parseColor("#502E05"))
                            tvStatus.isEnabled = true
                        }else{
                            tvStatus.text = "已领取"
                            tvStatus.background = null
                            tvStatus.setTextColor(Color.parseColor("#272727"))
                            tvStatus.isEnabled = false
                        }
                    }else{
                        if(holder.position==0){
                            tvStatus.text = item.ktjsName
                        }else{
                            tvStatus.text = item.mrzlName
                        }

                        tvStatus.isEnabled = false
                        tvStatus.background = null
                        tvStatus.setTextColor(Color.parseColor("#EEEEEE"))
                    }
                }

                if(item.day.isNullOrBlank()){
                    tvTime.text = "开通即领"
                }else{
                    tvTime.text =  "第${item.day}天"
                }
            }
        }

    }
}